package com.pmx.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.pmx.mall.product.entity.BrandEntity;
import com.pmx.mall.product.entity.CategoryEntity;
import com.pmx.mall.product.service.BrandService;
import com.pmx.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pmx.common.utils.PageUtils;
import com.pmx.common.utils.Query;

import com.pmx.mall.product.dao.CategoryBrandRelationDao;
import com.pmx.mall.product.entity.CategoryBrandRelationEntity;
import com.pmx.mall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        // 根据品牌编和分类编号查询出对应的名称 保存到关系表
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brandEntity = brandService.getById(brandId);
        CategoryEntity categoryEntity = categoryService.getById(catelogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        baseMapper.insert(categoryBrandRelation);
    }

    @Override
    public void updateBrandName(Long brandId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        LambdaUpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new LambdaUpdateWrapper<>();
        entity.setBrandId(brandId);
        entity.setBrandName(name);
        updateWrapper.eq(CategoryBrandRelationEntity::getBrandId, brandId);
        this.update(entity, updateWrapper);
    }

    @Override
    public void updateCatelogName(Long catId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        LambdaUpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new LambdaUpdateWrapper<>();
        entity.setCatelogId(catId);
        entity.setCatelogName(name);
        updateWrapper.eq(CategoryBrandRelationEntity::getCatelogId, catId);
        this.update(entity, updateWrapper);
    }

    /**
     * 根据类别编号查询所有的品牌信息
     * @param catId
     * @return
     */
    @Override
    public List<CategoryBrandRelationEntity> categoryBrandRelation(Long catId) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryBrandRelationEntity::getCatelogId, catId);
        //根据分类id查询出所有品牌信息
        List<CategoryBrandRelationEntity> list = categoryBrandRelationDao.selectList(queryWrapper);
//        List<BrandEntity> collect = list.stream().map((entity) -> {
//            BrandEntity brandEntity = new BrandEntity();
////            BeanUtils.copyProperties(entity, brandEntity); //表名不一致，不能直接拷贝bean
//            brandEntity.setName(entity.getBrandName());
//            brandEntity.setBrandId(entity.getBrandId());
//            return brandEntity;
//        }).collect(Collectors.toList());
        return list;
    }

}